package com.hf.eagle.web.struts.admin.manager;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.DeptInfo;
import com.hf.eagle.domain.UserInfo;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Pager;
import com.hf.eagle.web.base.SysConstant;

/**
 * @author Xu,XiaoYuan
 * @version 2011.9.15 01:35
 */
public class StaffWordAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.edit(mapping, form, request, response);
	}

	public ActionForward chooseUser(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		Pager pager = (Pager) dynaBean.get("pager");
		String user_name_like = (String) dynaBean.get("user_name_like");

		UserInfo userInfo = new UserInfo();
		userInfo.setIs_sys_user(SysConstant.IS_COMP_USER);
		userInfo.setIs_del(0);
		super.copyProperties(userInfo, form);
		userInfo.getMap().put("user_name_like", user_name_like);

		Long recordCount = super.getFacade().getUserInfoService().getUserInfoCount(userInfo);
		pager.init(recordCount, 30, pager.getRequestPage());
		userInfo.getRow().setFirst(pager.getFirstRow());
		userInfo.getRow().setCount(pager.getLastRow());

		List<UserInfo> list = super.getFacade().getUserInfoService().getUserInfoPaginatedList(userInfo);
		request.setAttribute("userInfoList", list);

		DeptInfo deptInfo = new DeptInfo();
		deptInfo.setIs_del(0);
		List<DeptInfo> deptInfoList = super.getFacade().getDeptInfoService().getDeptInfoList(deptInfo);
		request.setAttribute("deptInfoList", deptInfoList);

		return new ActionForward("/../hf/admin/manager/StaffWord/chooseUser.jsp");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		return mapping.findForward("input");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		UserInfo userInfo = new UserInfo();
		super.copyProperties(userInfo, form);
		userInfo.setUser_name(null);

		super.getFacade().getUserInfoService().modifyUserInfo(userInfo);
		saveMessage(request, "entity.updated");

		return this.edit(mapping, form, request, response);
	}

	public ActionForward getStaffWordContent(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String user_id = (String) dynaBean.get("user_id");
		String content = "";

		if (StringUtils.isNotBlank(user_id)) {
			UserInfo userInfo = new UserInfo();
			userInfo.setUser_id(Long.valueOf(user_id));
			userInfo.setIs_sys_user(SysConstant.IS_COMP_USER);
			userInfo.setIs_del(0);
			userInfo = super.getFacade().getUserInfoService().getUserInfo(userInfo);
			if (null != userInfo) {
				content = StringUtils.isBlank(userInfo.getStaff_word()) ? "" : userInfo.getStaff_word();
			}
		}

		super.renderText(response, content);
		return null;
	}
}
